<script setup lang="ts">
import { ref } from 'vue';

import { IconifyIcon } from '@vben/icons';

import { Button } from 'ant-design-vue';

interface ServiceItem {
  id: string;
  name: string;
  description: string;
  price: number;
  duration: number;
  contextId: string;
  status: number;
}

interface Props {
  services: ServiceItem[];
  contextId: string;
}

const props = defineProps<Props>();
const emit = defineEmits<{
  serviceSelected: [service: ServiceItem];
}>();

const selectedService = ref<null | ServiceItem>(null);

const selectService = (service: ServiceItem) => {
  selectedService.value = service;
  emit('serviceSelected', service);
};

const getStatusClass = (status: number) => {
  switch (status) {
    case 0: {
      return 'status-inactive';
    }
    case 1: {
      return 'status-active';
    }
    default: {
      return 'status-unknown';
    }
  }
};

const getStatusText = (status: number) => {
  switch (status) {
    case 0: {
      return '暂停服务';
    }
    case 1: {
      return '可预约';
    }
    default: {
      return '未知状态';
    }
  }
};
</script>

<template>
  <div class="service-list">
    <div class="service-grid">
      <div
        v-for="service in services"
        :key="service.id"
        class="service-card"
        :class="{ active: selectedService?.id === service.id }"
        @click="selectService(service)"
      >
        <div class="service-header">
          <h3 class="service-name">{{ service.name }}</h3>
          <div class="service-price">
            <span class="price-symbol">¥</span>
            <span class="price-value">{{ service.price }}</span>
          </div>
        </div>

        <div class="service-details">
          <p class="service-desc">{{ service.description }}</p>
          <div class="service-meta">
            <span class="duration">
              <IconifyIcon icon="mdi:clock-outline" />
              {{ service.duration }}分钟
            </span>
            <span class="status" :class="getStatusClass(service.status)">
              {{ getStatusText(service.status) }}
            </span>
          </div>
        </div>

        <div class="service-actions">
          <Button
            type="primary"
            size="small"
            @click.stop="selectService(service)"
          >
            选择服务
          </Button>
        </div>
      </div>
    </div>

    <div v-if="services.length === 0" class="empty-state">
      <IconifyIcon icon="mdi:package-variant" />
      <p class="empty-text">暂无服务项目</p>
    </div>
  </div>
</template>

<style scoped>
.service-list {
  width: 100%;
}

.service-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 20px;
}

.service-card {
  padding: 20px;
  cursor: pointer;
  background: white;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  transition: all 0.3s ease;
}

.service-card:hover {
  border-color: #3b82f6;
  box-shadow: 0 4px 12px rgb(59 130 246 / 10%);
}

.service-card.active {
  background: #f0f9ff;
  border-color: #3b82f6;
}

.service-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 12px;
}

.service-name {
  flex: 1;
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #1f2937;
}

.service-price {
  display: flex;
  align-items: baseline;
}

.price-symbol {
  margin-right: 2px;
  font-size: 14px;
  color: #ef4444;
}

.price-value {
  font-size: 24px;
  font-weight: 700;
  color: #ef4444;
}

.service-details {
  margin-bottom: 16px;
}

.service-desc {
  margin-bottom: 12px;
  font-size: 14px;
  line-height: 1.5;
  color: #6b7280;
}

.service-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.duration {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #6b7280;
}

.duration .anticon {
  margin-right: 4px;
}

.status {
  padding: 4px 8px;
  font-size: 12px;
  font-weight: 500;
  border-radius: 6px;
}

.status-active {
  color: #166534;
  background: #dcfce7;
}

.status-inactive {
  color: #dc2626;
  background: #fef2f2;
}

.status-unknown {
  color: #6b7280;
  background: #f3f4f6;
}

.service-actions {
  display: flex;
  justify-content: flex-end;
}

.empty-state {
  padding: 60px 20px;
  color: #6b7280;
  text-align: center;
}

.empty-icon {
  margin-bottom: 16px;
  opacity: 0.5;
}

.empty-text {
  margin: 0;
  font-size: 16px;
}
</style>
